#pragma once
#include <iostream>
#include <memory>
#include <functional>
#include <string>
#include "Protocol.hpp"

using namespace protocol_ns;
using calculate_t = std::function<std::shared_ptr<Responce>(const std::shared_ptr<Request>& req)>;

// 需要一个Req和一个Resp
class NetCal {
public:
    NetCal() {}

    std::shared_ptr<Responce> Calculator(const std::shared_ptr<Request>& req) {
        std::shared_ptr<Responce> Resp = std::make_shared<Responce>();
        int x = req->Get_X(), y = req->Get_Y();
        char oper = req->Get_oper();

        int& result = Resp->Get_result();
        int& code = Resp->Get_code();
        std::string& desc = Resp->Get_desc();

        switch (oper) {
            case '+':
                result = x + y;
                code = 0;
                desc = "success";
                break;
            case '-':
                result = x - y;
                code = 0;
                desc = "success";
                break;
            case '*':
                result = x * y;
                code = 0;
                desc = "success";
                break;  
            case '/':
                if (y == 0) {
                    code = 1;
                    desc = "div zero";
                } else {
                    code = 0;
                    desc = "success";
                    result = x / y;
                }
                break;
            case '%':
                if (y == 0) {
                    code = 2;
                    desc = "mod zero";
                } else {
                    code = 0;
                    desc = "success";
                    result = x % y;
                }
                break;     
            default:
                code = 2;
                desc = "illegal operation";
                break;     
        }
        return Resp;
    }

    ~NetCal() {}
private:

};